//引包

var a,b,c,d;

window.onload=function(){
	
	//动态加载更多用户信息
	var xmlrequest = createXHQ();
	
	xmlrequest.open("GET","../json/user_more.json");
	xmlrequest.send();
	
	xmlrequest.onreadystatechange=function(){
		if(xmlrequest.readyState===4){
			if(xmlrequest.status===200){
				var result = xmlrequest.responseText;
				
				var resultObject = JSON.parse(result);
				
				$().getId("usermoreID").html(resultObject.userID);
				$().getId("name1").value(resultObject.usenmae);
				$().getId("sex1").value(resultObject.sex);
				$().getId("birthday1").value(resultObject.birhtay);
				$().getId("email1").value(resultObject.email);
				$().getId("userID1").value(resultObject.IDcard);
				
			}else{
				alert("Error："+xmlrequest.status);				
			}
		}
	}
	
	//添加导航栏监听
	document.getElementById("user-t").onclick=function(){
		$().getId("nav-right-user").css("display","block");
		
		$().getId("nav-right-userUpdate").css("display","none");
		$().getId("nav-right-site").css("display","none");
		$().getId("nav-right-updatePas").css("display","none");
	}
	
	//添加导航栏监听
	document.getElementById("site-t").onclick=function(){
		$().getId("nav-right-site").css("display","block");
		
		
		$().getId("nav-right-userUpdate").css("display","none");
		$().getId("nav-right-user").css("display","none");
		$().getId("nav-right-updatePas").css("display","none");
		
		//动态加载更多用户信息
		var xmlrequestSite = createXHQ();
		
		xmlrequestSite.open("GET","../json/site.json");
		xmlrequestSite.send();
		
		xmlrequestSite.onreadystatechange=function(){
			
			if(xmlrequestSite.readyState===4){
			
				if(xmlrequestSite.status===200){
					var result = xmlrequestSite.responseText;
					
					var resultObject = JSON.parse(result);
					
					var nav_site = document.getElementById("nav-right-site");
					
					for(var i=0; i<resultObject.length; i++){
						var div1 = document.createElement("div");
						
						var tag ="<a href='#?id='"+resultObject[i].id+">删除</a>"+
								"<a href='#?id='"+resultObject[i].id+">修改</a>"+
								"<table>"+
									"<tr>"+
										"<td>姓名:</td><td>"+resultObject[i].name+"</td>"+
									"</tr>"+
									"<tr>"+
										"<td>地区:</td><td>"+resultObject[i].region+"</td>"+
									"</tr>"+
									"<tr>"+
										"<td>地址:</td><td>"+resultObject[i].address+"</td>"+
									"</tr>"+
									"<tr>"+
										"<td>电话:</td><td>"+resultObject[i].phone+"</td>"+
									"</tr>"+
								"</table>";
								
						div1.innerHTML = tag;
						nav_site.appendChild(div1);
					}
				
				}else{
					alert("Error："+xmlrequestSite.status);				
				}
			}
				
		}
		
	}
	
	//添加导航栏监听
	document.getElementById("pas-t").onclick=function(){
		$().getId("nav-right-updatePas").css("display","block");
		
		$().getId("nav-right-userUpdate").css("display","none");
		$().getId("nav-right-user").css("display","none");
		$().getId("nav-right-site").css("display","none");
	}
	
	//添加更多信息验证表单
	document.getElementById("names1").onkeyup=function(){
		a=varifyname(document.getElementById("names1").value);
	}
	
	document.getElementById("birthdays1").onkeyup=function(){
		b=varfiyBirhday(document.getElementById("birthdays1").value);
	}
	
	document.getElementById("emails1").onkeyup=function(){
		c=varfiyEmail(document.getElementById("emails1").value);
	}
	
	document.getElementById("userIDs1").onkeyup=function(){
		d=varfiyId(document.getElementById("userIDs1").value);
	}
	
	document.getElementById("more-oks1").onclick=function(){
		
		if(a&&b&&c&&d){
			
			var xmlRequest = createXHQ();
	
			xmlRequest.open("POST","url");
			
			xmlRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			
			var sexValue="";
			var sex = document.getElementsByName("sex");
			
			for(var i=0;i<sex.length;i++){
				if(sex[i].checked){
					sexValue=sex[i].value;
				}
			}
			
			var parameter = "name=" + document.getElementById("names1").value +"&sex=" + sexValue;
							+"&bithday=" + document.getElementById("birthdays1").value +"&email=" + document.getElementById("emails1").value
							+"&IDcard=" + document.getElementById("userIDs1").value ;
			
			xmlRequest.send(parameter);
			
			xmlRequest.onreadystatechange = function(){
				if(xmlRequest.readyState===4){
					if(xmlRequest.status===200){
						var ret = xmlRequest.responseText;
						var resultJ = JSON.parse(ret);
						
						alert(resultJ);
					}else{
						alert("错误代码："+xmlRequest.status);
					}
				}
			}
			
		}else{
			alert("请按要求填写表单");
		}
		
	}
	/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	
	var email1 = false;
	//验证邮箱
	document.getElementById("email1").onkeyup=function(){
		email1 = varfiyEmails(document.getElementById("email1").value,"i3s");
	}
	
	//修改邮箱信息
	document.getElementById("more-ok1").onclick=function(){
		
		if(email1){
			
			var xmlRequest = createXHQ();
	
			xmlRequest.open("POST","url");
			
			xmlRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			
			var parameter = "&email=" + document.getElementById("emails1").value ;
			
			xmlRequest.send(parameter);
			
			xmlRequest.onreadystatechange = function(){
				if(xmlRequest.readyState===4){
					if(xmlRequest.status===200){
						var ret = xmlRequest.responseText;
						var resultJ = JSON.parse(ret);
						
						alert(resultJ);
					}else{
						alert("错误代码："+xmlRequest.status);
					}
				}
			}
			
		}else{
			alert("请填写正确的邮箱");
		}
		
	}
	
	
	//添加修改密码验证++++++++++++++++++++++++++++++++++++++++++++++++++++
	var a1,b1;
	
	document.getElementById("updatepas").onkeyup=function(){
		
		var pas = document.getElementById("updatepas").value;
		
		a1 = varfiyPas(pas);
		
	}
	
	//密码是否一致
	document.getElementById("updatepas1").onkeyup=function(){
		
		var pas = document.getElementById("updatepas").value;
		
		var pas1 = document.getElementById("updatepas1").value;
		
		b1 = fitPas(pas,pas1);
		
	}
	
	document.getElementById("updateButton").onclick=function(){
		
		var pass = document.getElementById("password").value;
		
		if(pass!=null&&pass!=""){
			if(a1&&b1){
				
				var xmlRequest = createXHQ();
				
				var pasold = document.getElementById("password").value;
				var pas = document.getElementById("updatepas").value; 
		
				xmlRequest.open("GET","url?pasold="+pasold+"&pas="+pas);
				
				xmlRequest.send();
				
				xmlRequest.onreadystatechange = function(){
					if(xmlRequest.readyState===4){
						if(xmlRequest.status===200){
							var ret = xmlRequest.responseText;
							var resultJ = JSON.parse(ret);
							
							alert(resultJ);
						}else{
							alert("错误代码："+xmlRequest.status);
						}
					}
				}
				
			}else{
				alert("请按要求填写表单");
			}
		}else{
			alert("请输入原始密码");
		}
		
		
		
	}
	
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	
	//----------------------收货地址窗口设置-------------------------------------
	
	
	var siteWin ;
	//显示窗口
	document.getElementById("add").onclick=function(){
		
		//获取窗口位置
		var width = (document.documentElement.clientWidth-600)/2;
		var height = (document.documentElement.clientHeight-370)/2;
		
		siteWin = $().getId("add-site").css("display","block").css("position","absolute").css("top",height+"px").css("left",width+"px");
		
		openMB();
	}
	
	//窗口发生变动监听
	document.getElementsByTagName("body")[0].onresize=function(){
		
		//刷新窗口位置
		var width = (document.documentElement.clientWidth-600)/2;
		var height = (document.documentElement.clientHeight-370)/2;
		
		if(siteWin!=null){
			siteWin.css("top",height+"px").css("left",width+"px");
		}
		
		$().getId("menban").css("width",document.documentElement.scrollWidth +"px").css("height",document.documentElement.scrollHeight+"px");
	}
	
	//关闭窗口
	document.getElementById("delete-site").onclick=function(){
		$().getId("add-site").css("display","none");
		closeMB();
	}
	
	//手机号码验证
	var b1 = false;
	document.getElementById("phone").onkeyup=function(){
		b1 = varify(document.getElementById("phone").value);
	}
	//+++++++++++窗口验证++++++++++++++++++
	document.getElementById("site-ok").onclick=function(){
		
		var siteName = document.getElementById("site-name").value;
		var siteRegion = document.getElementById("site-region").value;
		var siteAdd = document.getElementById("site-add").value;
		
		
		if(siteName!=null&&siteName!=""){
			if(siteRegion!=null&&siteRegion!=""){
				if(siteAdd!=null&&siteAdd!=""){
					if(b1){
						
						//数据提交执行
						
					}else{
						alert("按要求填写");
					}
				}else{
					alert("详细地址不能为空");
				}
			}else{
				alert("地区不能为空");			
			}
		}else{
			alert("收货人姓名不能等于空");
		}
	}
}

function varifyname(name){
	
	//中文正则
	if(/[\u4e00-\u9fa5]{2,4}/.test(name)){
		document.getElementById("i1").innerHTML="";
		return true;
	}else{
		document.getElementById("i1").innerHTML="请输入2到4个中文字符";
		return false;
	}

}

//开启蒙版
function openMB(){
	//刷新窗口位置
	var width = document.documentElement.scrollWidth;
	var height = document.documentElement.scrollHeight;
	$().getId("menban").css("width",width+"px").css("height",height+"px").css("display","block");
}

//关闭蒙版
function closeMB(){
	$().getId("menban").css("display","none");
}

function varfiyBirhday(bithday){
	
	//日期的正则表达式
	var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
	var regExp = new RegExp(reg);
	if(!regExp.test(bithday)){
	　　document.getElementById("i2").innerHTML="请按格式输入日期";
		return false;
	}else{
		document.getElementById("i2").innerHTML="";
		return true;
	}
	
}

function varfiyEmail(email){
	
	var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); 
	
	if(!reg.test(email)){
		document.getElementById("i3").innerHTML="你输入的邮箱格式有误，请重新输入：";
		return false;
	}else{
		document.getElementById("i3").innerHTML="";
		return true;
	}
}	

function varfiyEmails(email,id){
	
	var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); 
	
	if(!reg.test(email)){
		document.getElementById(id).innerHTML="你输入的邮箱格式有误，请重新输入：";
		return false;
	}else{
		document.getElementById(id).innerHTML="";
		return true;
	}
}


function varfiyId(userID){
				
	var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
	
	 var result = true;
	 var iSum=0 ;
	 var info="" ;
	 if(!/^\d{17}(\d|x)$/i.test(userID)){
	 	document.getElementById("i4").innerHTML="你输入的身份证长度或格式错误";
	 	result = false;
	 } 
	 userID=userID.replace(/x$/i,"a");
	 if(aCity[parseInt(userID.substr(0,2))]==null){
	 	document.getElementById("i4").innerHTML="你的身份证地区非法";
	 	result = false;
	 }
	 sBirthday=userID.substr(6,4)+"-"+Number(userID.substr(10,2))+"-"+Number(userID.substr(12,2));
	 var d=new Date(sBirthday.replace(/-/g,"/")) ;
	 if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
	 	document.getElementById("i4").innerHTML="身份证上的出生日期非法";
	 	result = false;
	 }
	 for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(userID.charAt(17 - i),11) ;
	 if(iSum%11!=1){
	 	document.getElementById("i4").innerHTML="你输入的身份证号非法";
	 	result = false;
	 }
	
	 if(result){
	 	document.getElementById("i4").innerHTML="";
	 }
	 
	return result;
}

